package com.cwt_taxi_xa.custom_view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;

import com.cwt_taxi_xa.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by undi on 2019/6/21 17:50
 */
public class ConditioningView extends View {

    private Bitmap bitmap;
    public int width = 300;
    public int height = 100;
    int level = 0;
    List<Bitmap> windLevel = new ArrayList<>();

    public ConditioningView(Context context) {
        super(context);
        init();
    }

    public ConditioningView(Context context,AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ConditioningView(Context context,AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public void init() {
        //Bitmap src = BitmapFactory.decodeResource(getResources(), R.drawable.bg_conditioning);
        //将图片裁剪到指定大小
        //bitmap = Bitmap.(src, true);

        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bg_conditioning);
        width = bitmap.getWidth();
        height = bitmap.getHeight();
        windLevel.add( BitmapFactory.decodeResource(getResources(), R.drawable.ic_wind_level_1));
        windLevel.add( BitmapFactory.decodeResource(getResources(), R.drawable.ic_wind_level_2));
        windLevel.add( BitmapFactory.decodeResource(getResources(), R.drawable.ic_wind_level_3));
        windLevel.add( BitmapFactory.decodeResource(getResources(), R.drawable.ic_wind_level_4));
        windLevel.add( BitmapFactory.decodeResource(getResources(), R.drawable.ic_wind_level_5));
        windLevel.add( BitmapFactory.decodeResource(getResources(), R.drawable.ic_wind_level_6));
        windLevel.add( BitmapFactory.decodeResource(getResources(), R.drawable.ic_wind_level_7));
        windLevel.add( BitmapFactory.decodeResource(getResources(), R.drawable.ic_wind_level_8));
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec,heightMeasureSpec);
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //正在被拖动时则显示指定图片
        canvas.drawBitmap(bitmap, 0, 0, null);
        if(level > 0){
            for(int i = 0; i< level; i++){
                canvas.drawBitmap(windLevel.get(i), i * 64,height - windLevel.get(i).getHeight(), null);
            }
        }
    }

    public void selectLevel(int value){
        this.level = value;
        invalidate();
    }

}
